<template>
  <div>

    <div>
      <el-input style="width: 200px" placeholder="请输入名称"
                v-model="name"></el-input>
      <el-button style="margin: 5px; color: cadetblue" @click="search">搜索</el-button>
      <el-button style="margin-left: 0; color: #c5b8a5" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button style="color: cadetblue" @click="handleAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
        style="margin-left: 10px"
        confirm-button-text='确定'
        cancel-button-text='取消'
        icon="el-icon-info"
        icon-color="#a27e7e"
        title="您确定批量删除这些数据吗？"
        @confirm="handleDeleteBatch"
      >
        <el-button style="color: #cd5c5c" slot="reference" >批量删除<i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>

    <el-table :data="tableData" border stripe :header-cell-style="{backgroundColor: '#F2F2E6'}" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"></el-table-column>
      <el-table-column align="center" prop="id" label="ID" ></el-table-column>
      <el-table-column align="center" prop="name" label="角色名"></el-table-column>
      <el-table-column align="center" prop="flag" label="标识"></el-table-column>
      <el-table-column align="center" prop="description" label="描述"></el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <a-button style="color: cornflowerblue" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></a-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <el-dialog title="角色信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="60px" size="small">
        <el-form-item label="角色名:">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="标识:">
          <el-input v-model="form.flag" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="描述:">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button style="color: lightcoral" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="菜单分配" :visible.sync="menuDialogVis" width="25%">
      <el-tree
        :props="props"
        :data="menuData"
        show-checkbox
        node-key="id"
        ref="tree"
        :default-expanded-keys="expends"
        :default-checked-keys="checks">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span><i :class="data.icon"></i> {{ data.name }}</span>
        </span>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="menuDialogVis = false">取 消</el-button>
        <el-button style="color: lightcoral" @click="saveRoleMenu">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
    import request from "../../utils/request";

    export default {
        name: "Role",
      data(){
        return {
          name: '',
          search_name: '',
          tableData: [],
          total: 0,
          pageNum: 1,
          pageSize: 5,
          dialogFormVisible: false,
          menuDialogVis: false,
          multipleSelection: [],
          form: {},
          menuData: [],
          roleId: 0,
          menuIds: [],
          roleMenuId:{},
          expends: [],
          checks: [],
          props: {
            label: 'name',
          }
        }

      },
      created() {
          this.load()
      },
      methods:{
        handleSizeChange(pageSize) {
          this.pageSize = pageSize
          this.load()
        },
        handleCurrentChange(pageNum) {
          this.pageNum = pageNum
          this.load()
        },

        load() {
          // 请求分页查询
          request.get("/role/findPageByRolename", {
            params: {
              PageNum: this.pageNum,
              PageSize: this.pageSize,
              name: this.search_name,
            }
          }).then(res => {
            if (res.data !== null) {
              this.tableData = res.data.list
              this.total = res.data.total
            } else {
              this.tableData = []
            }
          })
        },

        search() {
          this.search_name = this.name
          this.load()
        },

        save() {
          request.post("/role", this.form).then(res => {
            if (res) {
              this.$message.success("保存成功!")
              this.dialogFormVisible = false
              this.load()
            } else {
              this.$message.error("保存失败!")
            }
          })
        },
        getKeys(list){
          let tmp = []
         for(let i = 0; i < list.length; i++)
           tmp.push(list[i].id)
         return tmp
        },

        saveRoleMenu(){
          this.roleMenuId ={
            "roleId": this.roleId,
            "menuIds": this.getKeys(this.$refs.tree.getCheckedNodes())
          }
          request.post("/role/roleMenu", this.roleMenuId).then(res => {
            // console.log(res)
            if(res){
              this.$message.success("菜单分配成功!")
              this.menuDialogVis = false
            }else{
              this.$message.error("菜单分配失败!")
            }

          })
        },

        handleAdd() {
          this.dialogFormVisible = true
          this.form = {}
          this.load()
        },

        handleEdit(row){
          this.form = row
          this.dialogFormVisible = true
        },

        handleDelete(id){
          request.delete("/role/" + id).then(res => {
            if (res) {
              this.$message.success("删除成功!")
              this.load()
            } else {
              this.$message.error("删除失败!")
            }
          })
        },

        handleSelectionChange(val){
          this.multipleSelection = val

        },

        handleDeleteBatch(){
          let ids = this.multipleSelection.map(v => v.id)
          if(ids.length === 0){
            this.$message.error("请选择要删除的数据!")
          }else {
            request.post("/role/del/batch", ids).then(res => {
              if (res) {
                this.$message.success("批量删除成功!")
                this.load()
              } else {
                this.$message.error("批量删除失败!")
              }
            })
          }
        },

        selectMenu(roleId){
          this.menuDialogVis = true
          this.roleId = roleId

          //请求菜单数据
          request.get("/menu").then(res => {
            this.menuData = res.data

            //把后台返回的菜单数据处理成id数组
            this.expends = this.menuData.map(v => v.id)
          })

          request.get("/role/getRoleMenu/" + this.roleId).then(res => {
            this.checks = res.data
          })

    },

        reset() {
          this.name = ''
          this.search_name = ''
          this.load()
        },
      }

    }
</script>

<style scoped>
  .v-application ul, .v-application ol {
    padding-left: 0 !important;
  }

</style>
